package com.hmall.common.config;


import com.hmall.common.interceptor.UserIofoInterceptor;
import com.hmall.common.utils.CollUtils;
import com.hmall.gateway.config.AuthProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
@RequiredArgsConstructor
//让指定类上的Config
@EnableConfigurationProperties(AuthProperties.class)
public class MvcConfig implements WebMvcConfigurer {

private final AuthProperties authProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
      //1.添加拦截器
        UserIofoInterceptor loginInterceptor = new UserIofoInterceptor();
        final InterceptorRegistration registration = registry.addInterceptor(loginInterceptor);
        //2.配置拦截路径
        List<String> includePaths = authProperties.getIncludePaths();
        if(CollUtils.isNotEmpty(includePaths)){
            registration.addPathPatterns(includePaths);
        }
        //3.配置放行路径
        List<String> excludePaths = authProperties.getExcludePaths();
        if(CollUtils.isNotEmpty(excludePaths)){
            registration.excludePathPatterns(excludePaths);
        }
        registration.excludePathPatterns(
                "/error",
                "/favicon.ico",
                "/v2/**",
                "/v3/**",
                "/swagger-resources/**",
                "/webjars/**",
                "/doc.html"
        );
    }
}
